import * as THREE from 'three'

// 海拔材质
export const elevationMaterial = new THREE.ShaderMaterial({
  vertexShader: `
    varying vec3 vWorldPosition;
    varying vec3 vPosition;
    void main() {
      vec4 worldPosition = modelMatrix * vec4(position, 1.0);
      vWorldPosition = worldPosition.xyz;
      vPosition = position;
      gl_Position = projectionMatrix * viewMatrix * worldPosition;
    }
  `,
  fragmentShader: `
    uniform vec3 topColor;
    uniform vec3 bottomColor;
    uniform float heightRange;
    varying vec3 vWorldPosition;
    varying vec3 vPosition;

    void main() {
      // 计算高度比例(0-1)
      float heightFactor = clamp(vPosition.z / heightRange, 0.0, 1.0);

      float contrastFactor = pow(heightFactor, 2.0); // 增加对比度
      // 使用三次平滑插值
      float smoothFactor = smoothstep(0.0, 1.0, contrastFactor);

      // 混合颜色
      vec3 finalColor = mix(bottomColor, topColor, smoothFactor);

      gl_FragColor = vec4(finalColor, 1.0);
    }
  `,
  side: THREE.DoubleSide,
  uniforms: {
    topColor: { value: new THREE.Color(0xeeeeee) }, // 高海拔颜色
    bottomColor: { value: new THREE.Color(0x333333) }, // 低海拔颜色
    heightRange: { value: 0.0 }, // 高度范围
  },
})
